home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / GETOPT.C < prev    next >
Text File  |  1993-08-09  |  2KB  |  85 lines

  1. /*
  2. **    @(#)getopt.c    2.2 (smail) 1/26/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  *
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24.  
  25. /* modified to save memory - DB3FL.9211xx */
  26.  
  27. #include "global.h"
  28. #include <string.h>
  29.  
  30. #ifdef    __TURBOC__
  31. #include <io.h>                    /* added by KA9Q for Turbo-C */
  32. #endif
  33.  
  34. int    optind = 1;
  35. static int optopt = 0;
  36. char *optarg;
  37.  
  38. int
  39. getopt(int argc,char **argv,char *opts)
  40. {
  41.     static int sp = 1;
  42.     int c;
  43.     char *cp;
  44.     static char err[] = "%s: Option '%c' %s\r\n";
  45.  
  46.     if(sp == 1) {
  47.         if(optind >= argc ||
  48.           argv[optind][0] != '-' || argv[optind][1] == '\0')
  49.             return(EOF);
  50.         else if(strcmp(argv[optind], "--") == 0) {
  51.             optind++;
  52.             return(EOF);
  53.         }
  54.     }
  55.     optopt = c = argv[optind][sp];
  56.  
  57.     if(c == ':' || (cp = strchr(opts,c)) == NULL) {
  58.         printf(err,argv[0],c,"unknown");
  59.         if(argv[optind][++sp] == '\0') {
  60.             optind++;
  61.             sp = 1;
  62.         }
  63.         return('?');
  64.     }
  65.     if(*++cp == ':') {
  66.         if(argv[optind][sp+1] != '\0') {
  67.             optarg = &argv[optind++][sp+1];
  68.         } else if(++optind >= argc) {
  69.             printf(err,argv[0],c,"requires an argument");
  70.             sp = 1;
  71.             return('?');
  72.         } else {
  73.             optarg = argv[optind++];
  74.         }
  75.         sp = 1;
  76.     } else {
  77.         if(argv[optind][++sp] == '\0') {
  78.             sp = 1;
  79.             optind++;
  80.         }
  81.         optarg = NULLCHAR;
  82.     }
  83.     return(c);
  84. }
  85.